//
//  TopHeaderView.m
//  WXMovie
//
//  Created by apple on 16/4/9.
//  Copyright © 2016年 zic_magic. All rights reserved.
//

#import "TopHeaderView.h"
#import <MediaPlayer/MediaPlayer.h>
#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVKit.h>

@implementation TopHeaderView
{
    AVPlayerViewController *moviePlayer;
    AVPlayer *player;
}
-(void)setModel:(TopHeaderModel *)model
{
    _model = model;
//    NSLog(@"%@", _model);

    [imgView sd_setImageWithURL:[NSURL URLWithString:_model.image]];
    titleLabel.text = _model.titleCn;
    directorLabel.text = _model.directors;
    actorLabel.text = _model.actors;
    typeLabel.text = _model.type;
    timeLabel.text = [NSString stringWithFormat:@"%@ %@", _model.time[@"location"], _model.time[@"date"]];
    
    
    

    UIImage *buttonImage1 = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:_model.videos[0][@"image"]]]];
    [videoButton1 setImage:buttonImage1 forState:UIControlStateNormal];

    UIImage *buttonImage2 = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:_model.videos[1][@"image"]]]];
    [videoButton2 setImage:buttonImage2 forState:UIControlStateNormal];

    UIImage *buttonImage3 = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:_model.videos[2][@"image"]]]];
    [videoButton3 setImage:buttonImage3 forState:UIControlStateNormal];
    
    UIImage *buttonImage4 = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:_model.videos[3][@"image"]]]];
    [videoButton4 setImage:buttonImage4 forState:UIControlStateNormal];
//    NSLog(@"%@", _model.videos[3][@"image"]);
    [self layoutIfNeeded];
}

- (IBAction)showMoviePlayer:(UIButton *)sender {
    
    NSInteger index = sender.tag - 1000;
    NSDictionary *dic = _model.videos[index];
    NSString *url = dic[@"url"];
//    NSLog(@"%@",url);
    
    moviePlayer = [[AVPlayerViewController alloc] init];
    
    
    player = [AVPlayer playerWithURL:[NSURL URLWithString:url]];
    
    moviePlayer.videoGravity = AVLayerVideoGravityResizeAspect;
    
    moviePlayer.player = player;
    
    moviePlayer.view.frame = self.bounds;
    
    NSLog(@"%@", url);
    
    
    NSLog(@"%ld", moviePlayer.player.status);
    
//    MPMoviePlayerViewController *moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:url]];
    
    

    [self addSubview:moviePlayer.view];
    
    [moviePlayer.player play];


    //[self.viewController presentViewController:moviePlayer animated:YES completion:NULL];
}



@end
